/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import br.com.systemcar.dao.UsuarioDao;
import br.com.systemcar.model.Usuario;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.Application;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
import javax.inject.Inject;

/**
 *
 * @author danilo
 */
public class ValidatorSenha implements Validator{
    
    Usuario user=new Usuario();
    
    @Inject
    UsuarioDao userDao;

    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        
        Application app =context.getApplication();
        String login =(String) app.createValueBinding("#{loginCtrl.login}").getValue(context);
        
        FacesMessage msg = 
		new FacesMessage("Usuario ou senha invalidos");
			msg.setSeverity(FacesMessage.SEVERITY_ERROR);
        user=userDao.findUser(login);
        
        String senha =value.toString();
        try {
            if(!util.Descriptografa.criptografar(senha).equals(user.getUsrSenha())){
                throw new ValidatorException(msg);
            }
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(ValidatorSenha.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    
}
